package com.kwok.openplatform.controller;

import com.github.pagehelper.PageInfo;
import com.kwok.openplatform.common.result.TableData;
import com.kwok.openplatform.entity.App;
import com.kwok.openplatform.entity.Customer;
import com.kwok.openplatform.service.AppService;
import com.kwok.openplatform.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 应用表(App)表控制层
 *
 * @author Kwok
 * @since 2024-07-21 18:31:36
 */
@RestController
@RequestMapping("/app")
public class AppController {

    @Autowired
    private AppService appService;

    @Autowired
    private CustomerService customerService;

    @GetMapping("/pageList")
    public TableData<App> pageList(Integer page, Integer limit, App app){
        PageInfo<App> pageInfo = appService.queryApps(page,limit,app);
        TableData<App> tableData = new TableData<>(0,"成功状态",pageInfo.getTotal(),pageInfo.getList());
        return tableData;
    }

    @PostMapping("/save")
    public String save(App app){
        Customer customer = customerService.queryCustomerById(app.getCustomerId());
        app.setCompanyName(customer.getCompany());
        Integer integer = appService.saveApp(app);
        return integer + "";
    }

    @PostMapping("/update")
    public String update(App app){
        //根据客户ID，拿到客户对象
        Customer customer = customerService.queryCustomerById(app.getCustomerId());
        //将客户对象中的公司名称赋值给应用对象
        app.setCompanyName(customer.getCompany());
        Integer integer = appService.updateAppById(app);
        return integer + "";
    }

    @DeleteMapping("/remove/{id}")
    public String remove(@PathVariable Integer id){
        Integer integer = appService.removeAppById(id);
        return integer + "";
    }

    @PostMapping("/batchDelete")
    public String delete(Integer... ids){
        Integer integer = appService.batchRemoveAppById(ids);
        return integer + "";
    }

}

